//
//  InputDeviceAdmViewController.m
//  SenMo
//
//  Created by ZengYuan on 16/1/24.
//  Copyright © 2016年 ZengYuan. All rights reserved.
//

#import "InputDeviceAdmViewController.h"
#import "DeviceHandle.h"

@interface InputDeviceAdmViewController ()
{
    MBProgressHUD *hud;
    
    __weak IBOutlet UIButton    *requestSeeBtn;

}
@end

@implementation InputDeviceAdmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    device_id_label.text = [NSString stringWithFormat:@"设备号：%@", self.device_id];
    
    [self.rightBtnPar setTitle:@"添加" forState:UIControlStateNormal];
    
    self.device_nameField.text = self.device_name?self.device_name:@"";
    
    UIImage *streImage = [[UIImage imageNamed:@"y_btn"] resizableImageWithCapInsets:UIEdgeInsetsMake(6, 10, 6, 10)];
    [requestSeeBtn setBackgroundImage:streImage forState:UIControlStateNormal];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)navRightAction{
    
    if(self.device_userField.text.length==0){
        MBALERT(@"用户名不能为空");
        return;
    }
    
    if(self.device_passField.text.length==0){
        MBALERT(@"密码不能为空");
        return;
    }
    
    //普通添加
    if (self.action_target == 0) {
        UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
        hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.labelText=@"正在绑定设备";
        
        __weak typeof(self) weakself = self;
        
        [DeviceHandle deviceBind:self.device_id device_username:self.device_userField.text device_pass:self.device_passField.text success:^{
            
            hud.mode = MBProgressHUDModeText;
            hud.labelText=@"绑定成功";
            [hud hide:YES afterDelay:1] ;
            
            [weakself.navigationController popToRootViewControllerAnimated:YES];
            
            [[NSNotificationCenter defaultCenter]
             postNotificationName:KHOME_FRESH_DEVICELIST_NOTI
             object:nil];
            
            
        } failure:^(NSString *error) {
            hud.mode = MBProgressHUDModeText;
            hud.labelText=error;
            [hud hide:YES afterDelay:1] ;
        }];
        
    }else{
        //重新设置用户名和密码
        UIWindow *window=[[UIApplication sharedApplication].windows lastObject];
        hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
        hud.labelText=@"正在保存";
        
        __weak typeof(self) weakself = self;
        
        [DeviceHandle deviceSetNewAdmi:self.device_id device_username:self.device_userField.text device_pass:self.device_passField.text success:^{
            
            hud.mode = MBProgressHUDModeText;
            hud.labelText=@"设置成功";
            [hud hide:YES afterDelay:1] ;
            
            [weakself.navigationController popToRootViewControllerAnimated:YES];
            
//            [[NSNotificationCenter defaultCenter]
//             postNotificationName:KHOME_FRESH_DEVICELIST_NOTI
//             object:nil];
            
            
        } failure:^(NSString *error) {
            hud.mode = MBProgressHUDModeText;
            hud.labelText=error;
            [hud hide:YES afterDelay:1] ;
        }];
        
    }
    
    

    
}
@end
